{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "#解决网络而分类问题\n",
    "\n",
    "import tensorflow as tf\n",
    "from numpy.random import RandomState #Numpy 科学计算工具包  这里通过numPy 工具包生成模拟数据\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "#定义训练数据batch 大小\n",
    "batch_size = 8\n",
    "\n",
    "#定义神经网络的参数\n",
    "w1 = tf.Variable(tf.random_normal([2,3],stddev=1)) # 2X3 的矩阵 方差不大于 1\n",
    "w2 = tf.Variable(tf.random_normal([3,1],stddev=1)) \n",
    "\n",
    "#在shape的一个维度上使用None 可以方便使用不指定的batch大小。\n",
    "#在训练时需要把数据分成的 batch，但是在测试时，可以一次性使用全部的数据。\n",
    "#当数据集比较小时这样比较方便测试，但数据集比较大时，将大量数据放入一个batch 可能导致内存溢出\n",
    "x = tf.placeholder(tf.float32,shape=(None,2),name='x-input')\n",
    "y_ = tf.placeholder(tf.float32,shape=(None,1),name='y-input')\n",
    "\n",
    "#定义神经网络前向传播的过程\n",
    "a = tf.matmul(x,w1)\n",
    "y = tf.matmul(a,w2)\n",
    "\n",
    "#定义损失函数\n",
    "cross_entropy = -tf.reduce_mean(y_*tf.log(tf.clip_by_value(y,1e-10,1.0)))\n",
    "\n",
    "#反向传播的算法\n",
    "train_step = tf.train.AdamOptimizer(0.001).minimize(cross_entropy)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "#通过随机数生成一个模拟数据集\n",
    "rdm = RandomState(1)\n",
    "dataset_size = 128\n",
    "X = rdm.rand(dataset_size,2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "训练之前的参数值。\n",
      "[[-0.46632287  0.52645755  0.8778314 ]\n",
      " [-1.38766587 -0.67949545  0.91447783]]\n",
      "[[ 0.24922276]\n",
      " [-1.25309455]\n",
      " [ 0.97426921]]\n",
      "After 0 training step(s),cross entropy on all data is 0.4552556872367859\n",
      "After 1000 training step(s),cross entropy on all data is 0.06228309124708176\n",
      "After 2000 training step(s),cross entropy on all data is 0.03248698264360428\n",
      "After 3000 training step(s),cross entropy on all data is 0.019915426149964333\n",
      "After 4000 training step(s),cross entropy on all data is 0.013260331936180592\n",
      "训练之后的参数值。\n",
      "[[-1.14148271 -0.39241478  1.84235561]\n",
      " [-2.21573997 -1.79381394  2.07820678]]\n",
      "[[-0.74409777]\n",
      " [-2.49662995]\n",
      " [ 2.05670333]]\n"
     ]
    }
   ],
   "source": [
    "#定义规则给出样本的标签。在这里所有x1+x2<1的样例都被认为时正样本而其他为负样本。\n",
    "#在这里是哦用0 来标识负样本，1来标识正样本。大部分解决分类问题的神经网络都会采用0和1表示\n",
    "Y=[[int(x1+x2<1)] for (x1,x2) in X ]\n",
    "\n",
    "# 创建一个回话来运行TensorFlow\n",
    "with tf.Session() as sess:\n",
    "    init_op = tf.global_variables_initializer()\n",
    "    #初始化变量\n",
    "    sess.run(init_op)\n",
    "    print(\"训练之前的参数值。\")\n",
    "    print(sess.run(w1))\n",
    "    print(sess.run(w2))\n",
    "\n",
    "    # 设定训练的轮数\n",
    "    STEPS = 5000\n",
    "    for i in range(STEPS):\n",
    "        #每次选取batch_size 个样本进行训练\n",
    "        start = (i * batch_size) % dataset_size\n",
    "        end = min(start+batch_size,dataset_size)\n",
    "\n",
    "        #通过选取样本训练神经网络并更新参数\n",
    "        sess.run(train_step,feed_dict={x:X[start:end],y_:Y[start:end]})\n",
    "        if i % 1000 ==0:\n",
    "            # 每隔一段时间计算所有数据上的交叉熵并输出\n",
    "            total_cross_entropy = sess.run(cross_entropy,feed_dict={x:X,y_:Y})\n",
    "            print(\"After {0} training step(s),cross entropy on all data is {1}\".format(i,total_cross_entropy))\n",
    "    \n",
    "    print(\"训练之后的参数值。\")\n",
    "    print(sess.run(w1))\n",
    "    print(sess.run(w2))\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
